package com.douya.middleware.config;

import cn.hutool.core.collection.CollectionUtil;
import com.douya.middleware.property.DouYaCacheProperties;
import com.douya.middleware.property.RedisProperties;
import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.ClusterServersConfig;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

import javax.annotation.Resource;
import java.util.List;

/**
 * 说明：redis配置
 *
 * @author 胡晨烨
 * @date 2022/10/22
 */
@Configuration
@EnableCaching
@ConditionalOnProperty(name = "douya.middleware.redis.enable", havingValue = "true")
public class RedisConfig {

    @Resource
    DouYaCacheProperties douYaCacheProperties;

    private static final String NODE_PREFIX = "redis://";

    @Bean
    @Lazy
    public RedissonClient redissonClientBuilder() {
        RedisProperties redis = douYaCacheProperties.getRedis();
        // 如果redis配置为空，则不初始化
        if (redis == null) {
            return Redisson.create();
        }
        String redisNodes = redis.getNodes();
        if (StringUtils.isBlank(redisNodes)) {
            return Redisson.create();
        }
        Integer timeout = redis.getTimeout();
        Config config = new Config();
        config.setCodec(new org.redisson.client.codec.StringCodec());
        // 如果有逗号分隔的节点，则使用集群模式
        if (redisNodes.contains(",")) {
            List<String> nodes = StringUtils.isBlank(redisNodes) ? Lists.newArrayList() : Lists.newArrayList(redisNodes.split(","));
            if (CollectionUtil.isEmpty(nodes)) {
                return Redisson.create();
            }
            List<String> nodeList = Lists.newArrayList();
            for (String node : nodes) {
                nodeList.add(NODE_PREFIX + node);
            }
            ClusterServersConfig clusterServersConfig = config.useClusterServers()
                    .addNodeAddress(nodeList.toArray(new String[0]));
            // 设置对于master节点的连接池中最大连接数
            clusterServersConfig.setMasterConnectionPoolSize(12);
            // 设置对于slave节点的连接池中最大连接数
            clusterServersConfig.setSlaveConnectionPoolSize(3);
            clusterServersConfig.setSlaveConnectionMinimumIdleSize(3);
            clusterServersConfig.setConnectTimeout(timeout);
            clusterServersConfig.setTimeout(timeout);
            clusterServersConfig.setPassword(redis.getPassword());
        } else {
            SingleServerConfig singleServerConfig = config.useSingleServer();
            singleServerConfig.setAddress(NODE_PREFIX + redisNodes);
            singleServerConfig.setConnectionPoolSize(3);
            singleServerConfig.setConnectionMinimumIdleSize(3);
            singleServerConfig.setConnectTimeout(timeout);
            singleServerConfig.setTimeout(timeout);
            singleServerConfig.setPassword(redis.getPassword());
        }
        return Redisson.create(config);
    }


}
